const associationDao = require('../service/association_dao')
const fs = require('fs')

module.exports = {
  // 协会列表
  associationlist: async (ctx, next) => {
    let body = await associationDao.getAllAssociation();
    await ctx.render('association/associationlist', {
      body: body
    })
  },

  edit: async (ctx, next) => {
    const id = ctx.request.query.id;
    const association = await associationDao.getAssociationInfo(id);
    await ctx.render('association/edit', {
      associationResult: association
    });
  },

  doEdit: async (ctx, next) => {
    if (ctx.request.body.logoVal) {
      let base64Data = ctx.request.body.logoVal.replace(/^data:image\/\w+;base64,/, "");
      let dataBuffer = Buffer.from(base64Data, 'base64');
      let getName = Number(Math.random().toString().substr(3)).toString(36) + Date.now();
      let upload = await new Promise((reslove, reject) => {
        fs.writeFile('./public/uploads/' + getName + '.png', dataBuffer, err => {
          if (err) {
            throw err;
            reject(false);
          };
          reslove(true)
          console.log("图片上传成功")
        });
      });
      console.log('123123312321')
      if (upload) {
        let id = ctx.request.body.id;
        let association_name = ctx.request.body.association_name;
        let contactPerson = ctx.request.body.contactPerson;
        let contactMobile = ctx.request.body.contactMobile;
        let introduce = ctx.request.body.introduce;
        let status = ctx.request.body.status;
        let frozenRemark = ctx.request.body.frozenRemark;
        let remark = ctx.request.body.remark;
        let auditOptions = ctx.request.body.auditOptions;
        let logo = getName + '.png';
        let json = {
          id,
          association_name,
          contactPerson,
          contactMobile,
          introduce,
          status,
          frozenRemark,
          remark,
          auditOptions,
          logo
        };
        console.log(json)
        await associationDao.putAssociation(json);      
        await ctx.redirect('/associations/associationlist', {})

      }

    }
  },

  frozen: async (ctx, next) => {
    const id = ctx.request.query.id;
    const association = await associationDao.getAssociationInfo(id);
    await ctx.render('association/frozen', {
      associationResult: association
    });
  },

  doFrozen: async (ctx, next) => {
    var editResult = ctx.request.body;
    associationDao.putAssociation(editResult);
    await ctx.redirect('/associations/associationlist', {})
  },


  auditlist: async (ctx, next) => {
    let body = await associationDao.getAllAssociation();
    await ctx.render('association/auditlist', {
      body: body
    })
  },

  detail: async (ctx, next) => {
    const id = ctx.request.query.id;
    const association = await associationDao.getAssociationInfo(id);
    await ctx.render('association/detail', {
      associationResult: association
    });
  },

  doDetail: async (ctx, next) => {
    var editResult = ctx.request.body;
    associationDao.putAssociation(editResult);
    await ctx.redirect('/associations/auditlist', {})
  },
}